package com.bss.service;

import java.util.List;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.andy.basics.Dao;
import com.bss.dao.AdmissionDao;
import com.bss.dao.CurrentAttendentDao;
import com.bss.model.Admission;
import com.bss.model.CurrentAttendent;
import com.bss.model.Payment;

@Service
@Scope("session")
public class IdGenerationService extends com.andy.basics.Service<Admission> {

	private DataModel data;

	private Integer utensilDeposit;

	private Integer securityDeposit;

	public DataModel getData() {
		return this.data;
	}

	@Autowired
	private AdmissionDao admissionDao;

	@Autowired
	private CurrentAttendentDao currentAttendentDao;

	@Autowired
	private AdmissionService admissionService;

	@Transactional
	public DataModel getData(final String patientName) {
		List<Admission> admissions = this.admissionDao.findForPatientName(patientName.toString());
		this.data = new ListDataModel(admissions);
		return this.data;
	}

	@Transactional
	public DataModel getCurrentAdmissionData(final String patientName) {
		List<Admission> admissions = this.admissionDao.findCurrentAdmissions(patientName.toString());
		this.data = new ListDataModel(admissions);
		return this.data;
	}

	@Transactional
	public DataModel getActiveAdmissionData(final String patientName) {
		List<Admission> admissions = this.admissionDao.findActiveAdmissios(patientName.toString());
		this.data = new ListDataModel(admissions);
		return this.data;
	}

	@Override
	public Dao<Admission> getDao() {
		return this.admissionDao;
	}

	@Transactional
	public void select() {

	}

	@Transactional
	public void loadModelFromId(final Integer id) {
		Admission admission = this.admissionDao.findById(id);
		List<CurrentAttendent> attendents = this.currentAttendentDao
				.find("dateOfArrival is not null and dateOfLeave is null and admission.id=" + id);
		admission.setCurrentAttendents(attendents);
		admission.getBedBookings().size();
		admission.getRoomBookings().size();
		admission.getMaterialAllocations().size();
		admission.getPayments().size();

		this.securityDeposit = 0;
		this.utensilDeposit = 0;

		for (Payment payment : admission.getPayments()) {
			if (payment.getPaymentType().getName().equals("Security Deposit")) {
				if (!payment.isRefund()) {
					this.securityDeposit += payment.getAmount();
				} else {
					this.securityDeposit -= payment.getAmount();
				}
			}
			if (!payment.isRefund() && payment.getPaymentType().getName().equals("Utensil Deposit")) {
				if (!payment.isRefund()) {
					this.utensilDeposit += payment.getAmount();
				} else {
					this.utensilDeposit -= payment.getAmount();
				}
			}
		}

		if (this.utensilDeposit < 0) {
			this.utensilDeposit = 0;
		}
		if (this.securityDeposit < 0) {
			this.securityDeposit = 0;
		}

		this.admissionService.populateAdmission(admission);

		this.setModel(admission);
	}

	public Integer getUtensilDeposit() {
		return this.utensilDeposit;
	}

	public void setUtensilDeposit(Integer utensilDeposit) {
		this.utensilDeposit = utensilDeposit;
	}

	public Integer getSecurityDeposit() {
		return this.securityDeposit;
	}

	public void setSecurityDeposit(Integer securityDeposit) {
		this.securityDeposit = securityDeposit;
	}

	@Transactional
	public void updateUtensilSet() {
		Admission a = this.getModel();
		Admission admission = this.admissionDao.findById(a.getId());

		admission.setMaterialSet(a.getMaterialSet());

		this.admissionDao.update(admission);

	}
}
